using UnityEngine;

public class ArmorItem : EquipmentItem
{
	public GameObject torsTop;

	public GameObject armLeftTop;

	public GameObject armRightTop;

	public GameObject armLeftDown;

	public GameObject armRightDown;

	public GameObject legLeftTop;

	public GameObject legRightTop;

	public GameObject legLeftDown;

	public GameObject legRightDown;

	public void SetLayer(int val)
	{
		SetLayerForObj(torsTop, val);
		SetLayerForObj(armLeftTop, val);
		SetLayerForObj(armRightTop, val);
		SetLayerForObj(armLeftDown, val);
		SetLayerForObj(armRightDown, val);
		SetLayerForObj(legLeftTop, val);
		SetLayerForObj(legRightTop, val);
		SetLayerForObj(legLeftDown, val);
		SetLayerForObj(legRightDown, val);
	}

	public void SetLocalScale(Vector3 val)
	{
		if (torsTop != null)
		{
			torsTop.transform.localScale = val;
		}
		if (armLeftTop != null)
		{
			armLeftTop.transform.localScale = val;
		}
		if (armRightTop != null)
		{
			armRightTop.transform.localScale = val;
		}
		if (armLeftDown != null)
		{
			armLeftDown.transform.localScale = val;
		}
		if (armRightDown != null)
		{
			armRightDown.transform.localScale = val;
		}
		if (legLeftTop != null)
		{
			legLeftTop.transform.localScale = val;
		}
		if (legRightTop != null)
		{
			legRightTop.transform.localScale = val;
		}
		if (legLeftDown != null)
		{
			legLeftDown.transform.localScale = val;
		}
		if (legRightDown != null)
		{
			legRightDown.transform.localScale = val;
		}
	}

	public void EquipOn(EquipmentManager needPlayer)
	{
		EquipPartArmor(torsTop, needPlayer.ArmorTors);
		EquipPartArmor(armLeftTop, needPlayer.ArmorArmLeftTop);
		EquipPartArmor(armRightTop, needPlayer.ArmorArmRightTop);
		EquipPartArmor(armLeftDown, needPlayer.ArmorArmLeftDown);
		EquipPartArmor(armRightDown, needPlayer.ArmorArmRightDown);
		EquipPartArmor(legLeftTop, needPlayer.ArmorLegLeftTop);
		EquipPartArmor(legRightTop, needPlayer.ArmorLegRightTop);
		EquipPartArmor(legLeftDown, needPlayer.ArmorLegLeftDown);
		EquipPartArmor(legRightDown, needPlayer.ArmorLegRightDown);
	}

	private void EquipPartArmor(GameObject partArmor, Transform pointAnchor)
	{
		if (partArmor != null)
		{
			partArmor.transform.parent = pointAnchor;
			partArmor.transform.localPosition = Vector3.zero;
			partArmor.transform.localRotation = Quaternion.identity;
		}
	}

	private void SetLayerForObj(GameObject curObj, int layerVal)
	{
		if (curObj != null)
		{
			Transform[] componentsInChildren = curObj.GetComponentsInChildren<Transform>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				componentsInChildren[i].gameObject.layer = layerVal;
			}
		}
	}

	private void OnDestroy()
	{
		if (torsTop != null)
		{
			Object.Destroy(torsTop);
		}
		if (armLeftTop != null)
		{
			Object.Destroy(armLeftTop);
		}
		if (armRightTop != null)
		{
			Object.Destroy(armRightTop);
		}
		if (armLeftDown != null)
		{
			Object.Destroy(armLeftDown);
		}
		if (armRightDown != null)
		{
			Object.Destroy(armRightDown);
		}
		if (legRightTop != null)
		{
			Object.Destroy(legRightTop);
		}
		if (legLeftTop != null)
		{
			Object.Destroy(legLeftTop);
		}
		if (legLeftDown != null)
		{
			Object.Destroy(legLeftDown);
		}
		if (legRightDown != null)
		{
			Object.Destroy(legRightDown);
		}
	}
}
